<?php 

    include '/public/sql_pdo_lib.php';
    header('Content-type: application/json');
    ini_set('display_errors','ON');
    error_reporting(E_ALL);
    ini_set('max_execution_time', 10);

    // try catch
    function geturldata($url,$out_time="10") { 
        $ch = curl_init(); 
        curl_setopt ($ch, CURLOPT_URL, $url); 
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 1); 
        curl_setopt($ch, CURLOPT_PROXY, 'http://127.0.0.1:1080'); 
        curl_setopt ($ch, CURLOPT_TIMEOUT_MS, $out_time);
        $content = curl_exec($ch); 
        return $content; 
    }

    $pic_content = geturldata('http://baidu.com');

    //创建可抛出一个异常的函数
    function checkYes($return){
        if($return==""){
            throw new Exception("运行 catch ");
        }
        return true;
    }

    //在 "try" 代码块中触发异常
    try{
        checkYes($pic_content);
        print_r("已经接通了");
        // $location_url = realpath('allImage');
        // $dir = iconv("UTF-8", "GBK", "allImage/".$file_name);
        // if (!file_exists($dir)){
        //     mkdir ($dir,0777,true);
        //     file_put_contents($location_url.'/'.$file_name.'/'.$pic_name, $pic_content);
        // }else{
        //     file_put_contents($location_url.'/'.$file_name.'/'.$pic_name, $pic_content);
        // }
        // $id = $postData['data']["id"];

        // $record = array("image_download"=> '2');
        // $where_sql = "where id = :id";
        // $bind_var=array("id"=>$id);

        // $new=pdo_fn_bool_update($db_name, "works_code", $record, $where_sql, $bind_var);
    }

    //捕获异常
    catch(Exception $e){
        // $pic_content = geturldata($image_url);
        
        // $location_url = realpath('allImage');
        // $dir = iconv("UTF-8", "GBK", "allImage/".$file_name);
        // if (!file_exists($dir)){
        //     mkdir ($dir,0777,true);
        //     file_put_contents($location_url.'/'.$file_name.'/'.$pic_name, $pic_content);
        // }else{
        //     file_put_contents($location_url.'/'.$file_name.'/'.$pic_name, $pic_content);
        // }
        // $id = $postData['data']["id"];

        // $record = array("image_download"=> '2');
        // $where_sql = "where id = :id";
        // $bind_var=array("id"=>$id);

        // $new=pdo_fn_bool_update($db_name, "works_code", $record, $where_sql, $bind_var);        
        print_r("tong");
     }




    // 写入文件

    // $filename = 'file.txt';
    // $word = "你好!\r\n";

    // $fh = fopen($filename, "a");
    // fwrite($fh, $word);
    // fclose($fh);


?>
